package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.PageResult;
import com.pinyougou.mapper.BrandMapper;
import com.pinyougou.pojo.Brand;
import com.pinyougou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service(interfaceName = "com.pinyougou.service.BrandService")
@Transactional
public class BrandServiceImpl implements BrandService
{
    /**注入数据访问接口代理对象*/

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 多条件分页查询
     *
     * @param brand
     * @param page
     * @param rows
     */
    @Override
    public PageResult findByPage(Brand brand, int page, int rows)
    {
        PageInfo<Brand> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect()
        {
            @Override
            public void doSelect()
            {
                brandMapper.findByLike(brand);
            }
        });
        System.out.println("总记录数 = " + pageInfo.getTotal());
        System.out.println("总页数 = " + pageInfo.getPages());


        return new PageResult(pageInfo.getPages(),pageInfo.getList());
    }

    /** 查找所有的品牌名称和id */
    @Override
    public List<Map<String, Object>> findIdNameList()
    {
        return brandMapper.findIdNameList();
    }


    /**
     * 添加方法
     *
     * @param brand
     */
    @Override
    public void save(Brand brand)
    {
        //insert和insertSelective的区别insertSelective会根据传过来的属性生成insert语句，而insert是生成全部字段的insert语句
        brandMapper.insertSelective(brand);

    }

    /**
     * 修改方法
     *
     * @param brand
     */
    @Override
    public void update(Brand brand)
    {
        //updateByPrimaryKeySelective方法中当brand为null时，不会执行update方法
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id)
    {

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteAll(Serializable[] ids)
    {
        /*for (Serializable id : ids)
        {
            brandMapper.deleteByPrimaryKey(id);
        }*/
        //批量删除
        //创建示例对象
        Example example = new Example(Brand.class);
        //创建条件对象
        Example.Criteria criteria=example.createCriteria();
        //in条件
        criteria.andIn("id", Arrays.asList(ids));
        //删除
        brandMapper.deleteByExample(example);
    }

    /**
     * 根据主键id查询
     *
     * @param id
     */
    @Override
    public Brand findOne(Serializable id)
    {
        return null;
    }

    /**
     * 查询所有品牌
     */
    @Override
    public List<Brand> findAll()
    {
        return brandMapper.selectAll();
    }


}